const { Service } = require("egg");

class RoleService extends Service {
    /**
     * 获取角色列表
     */
    async readList() {
        let sql = "select * from role";
        let res = await this.app.mysql.query(sql);
        return res;
    }

    /**
     * 根据角色名称，获取角色
     * @param {string} name 角色名字
     */
    async findRoleByName(name) {
        let sql = "select * from role where name = ? limit 0, 1";
        let params = [name];
        let res = await this.app.mysql.query(sql, params);
        return res[0];
    }

    /**
     * 添加一个角色
     * @param {string} name 角色名字
     */
    async create(name) {
        let sql = "insert into role (name, aid) values (?, ?)";
        let params = [name, 0];
        let res = await this.app.mysql.query(sql, params);
        return res;
    }
}

module.exports = RoleService;
